package de.openkanban.client.ui.board;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.AnimationCallback;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
import com.smartgwt.client.widgets.form.fields.events.FocusEvent;
import com.smartgwt.client.widgets.form.fields.events.FocusHandler;
import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.layout.VLayout;

import de.openkanban.client.Main;
import de.openkanban.client.util.RootPanel;

/**
 * GUI Element for the EnterBoardNameView
 */
public class EnterBoardNameView extends HLayout {

	private static final String DEFAULT_TEXT_VALUE = "Enter board name";

	private DynamicForm boardNameForm;
	private TextItem boardNameTextItem;
	private IButton cancelButton;

	public EnterBoardNameView() {
		boardNameForm = new DynamicForm();
		boardNameTextItem = new TextItem();

		VLayout vLayout = new VLayout();
		HLayout hLayout = new HLayout();

		Layout placeholderLayout = new HLayout();
		placeholderLayout.setHeight("*");

		boardNameTextItem.setWidth(300);
		boardNameTextItem.setHeight(30);
		boardNameTextItem.setAlign(Alignment.CENTER);
		boardNameTextItem.setRequired(true);
		boardNameTextItem.setShowTitle(false);
		boardNameTextItem.setShowFocused(true);
		boardNameTextItem.setTextBoxStyle("enterBoardNameTextItem");
		boardNameTextItem.setValue(DEFAULT_TEXT_VALUE);

		boardNameTextItem.addFocusHandler(new FocusHandler() {
			@Override
			public void onFocus(FocusEvent event) {
				if (DEFAULT_TEXT_VALUE.equals(boardNameTextItem.getValueAsString())) {
					boardNameTextItem.clearValue();
				}
			}
		});
		boardNameTextItem.addBlurHandler(new BlurHandler() {
			@Override
			public void onBlur(BlurEvent event) {
				if (boardNameTextItem.getValueAsString() == null || "".equals(boardNameTextItem.getValueAsString().trim())) {
					boardNameTextItem.setValue(DEFAULT_TEXT_VALUE);
				}
			}
		});
		boardNameTextItem.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if ("Enter".equals(event.getKeyName()) && boardNameForm.validate()) {
					
					RootPanel.main.getBoard().animateFade(0, new AnimationCallback() {
						@Override
						public void execute(boolean earlyFinish) {
							RootPanel.main.getBoard().getCurrentBoardNameLabel()
									.setContents(boardNameTextItem.getValueAsString());
							RootPanel.main.getBoard().showCurrentBoardNameLabel();
							RootPanel.main.getBoard().createBoard(boardNameTextItem.getValueAsString());
							RootPanel.main.getBoard().getBoardHeaderHLayout().setVisible(true);

							RootPanel.main.getBoard().getWhiteBoardView().removeActivitiesHLayoutMembers();

							RootPanel.main.getBoard().animateFade(100, null, Main.DEFAULT_FADE_DURATION);
						}
					}, Main.DEFAULT_FADE_DURATION);
				}
			}
		});

		boardNameForm.setFields(boardNameTextItem);
		
		cancelButton = new BoardHeaderButton("cancel");
		cancelButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				RootPanel.activateBoardEditMode(false);
			}
		});

		Layout placeHolderLeft = new Layout();
		Layout placeHolderRight = new Layout();
		placeHolderLeft.setWidth("*");
		placeHolderRight.setWidth("*");

		hLayout.addMember(boardNameForm);
		if (RootPanel.main.getBoard().getWhiteBoardView().getBoardDto().getBoardId() != null) {
			hLayout.addMember(cancelButton);
		}

		vLayout.addMember(placeholderLayout);
		vLayout.addMember(hLayout);
		vLayout.addMember(placeholderLayout);

		addMember(placeHolderLeft);
		addMember(vLayout);
		addMember(placeHolderRight);
	}

	public void showCancelButton() {
		cancelButton.show();
	}

	public void hideCancelButton() {
		cancelButton.hide();
	}

}
